﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	internal static class GenParse_UserData_50_DateAndTime
	{
		#region private constants

		private const int NrOfBytes_WithoutSecond = 8;
		private const int NrOfBytes_WithSecond = 9;

		#endregion private constants

		#region internal methods

		internal static byte[] GenerateBytes(UserData userData)
		{
			byte[] userDataBytes = null;

			if (userData is UserData_50_DateAndTime)
			{
				UserData_50_DateAndTime userData50 = userData as UserData_50_DateAndTime;

				if (userData50.IncludingSecond)
				{
					userDataBytes = new byte[NrOfBytes_WithSecond];
					userDataBytes[0] = NrOfBytes_WithSecond - GenParse_UserData.HeaderLength;	// user data length
				}
				else
				{
					userDataBytes = new byte[NrOfBytes_WithoutSecond];
					userDataBytes[0] = NrOfBytes_WithoutSecond - GenParse_UserData.HeaderLength;	// user data length
				}
				
				userDataBytes[1] = UserData.UserData50DateAndTime;
				userDataBytes[2] = userData50.Year;
				userDataBytes[3] = userData50.Century;
				userDataBytes[4] = userData50.Month;
				userDataBytes[5] = userData50.Day;
				userDataBytes[6] = userData50.Hour;
				userDataBytes[7] = userData50.Minute;

				if (userData50.IncludingSecond)
				{
					userDataBytes[8] = userData50.Second;
				}
			}
			else
			{
				// TODO handle error
			}

			return userDataBytes;
		}

		internal static bool ParseBytes(byte[] userDataBytes, out UserData userData50DateAndTime, out int consumedBytes)
		{
			userData50DateAndTime = null;
			consumedBytes = 0;

			if (userDataBytes.Length >= NrOfBytes_WithoutSecond)
			{
				// first byte
				byte setLength = userDataBytes[0];
				if (userDataBytes.Length >= setLength + 2)
				{
					if (
						(setLength == NrOfBytes_WithoutSecond - GenParse_UserData.HeaderLength)
						|| (setLength == NrOfBytes_WithSecond - GenParse_UserData.HeaderLength)
						)
					{
						if (userDataBytes[1] == UserData.UserData50DateAndTime)
						{
							byte year = userDataBytes[2];
							byte century = userDataBytes[3];
							byte month = userDataBytes[4];
							byte day = userDataBytes[5];
							byte hour = userDataBytes[6];
							byte minute = userDataBytes[7];

							if (setLength == NrOfBytes_WithSecond - GenParse_UserData.HeaderLength)
							{
								byte second = userDataBytes[8];
								consumedBytes = NrOfBytes_WithSecond;
								userData50DateAndTime = new UserData_50_DateAndTime(century, year, month, day, hour, minute, second);
							}
							else
							{
								consumedBytes = NrOfBytes_WithoutSecond;
								userData50DateAndTime = new UserData_50_DateAndTime(century, year, month, day, hour, minute);
							}				

							return true;
						}

						// wrong value for the data type
						return false;
					}

					// TODO handle error (wrong value in first byte the length)
					return false;
				}

				// TODO handle error: not enough bytes as specified by the SL value
				return false;
			}

			// TODO handle error (not enough bytes)
			return false;
		}

		#endregion internal methods
	}
}
